package com.ncloud.resman.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.PageHelperUtils;
import com.ncloud.resman.constant.ResConstant;
import com.ncloud.resman.entity.Brand;
import com.ncloud.resman.mapper.BrandMapper;
import com.ncloud.resman.service.BrandService;
import com.ncloud.resman.service.SequenceService;

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private BrandMapper mapper;
	@Autowired
	private SequenceService sequenceService;
	
	@Override
	public Object getBrandList(JSONObject json) {
		Object object = null;
		String eqptType = json.getString("eqptType");
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<Map<String, Object>> brandList = mapper.findBrandListByType(eqptType);
			@SuppressWarnings({ "rawtypes", "unchecked" })
			PageInfo pageInfo = new PageInfo(brandList);
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else{
			List<Map<String, Object>> brandList = mapper.findBrandListByType(eqptType);
			object = JSON.parseArray(JSON.toJSONString(brandList));
		}
		return object;
	}

	@Override
	public int insertBrand(Brand brand) {
		//判断是否存在同名品牌
		List<Brand> brandList = mapper.getBrandByEqptTypeAndBrandName(brand.getEqptType(), brand.getBrandName());
		if(brandList!=null && !brandList.isEmpty()){
			throw new UserException("已经存在同名品牌！【eqptType:"+brand.getEqptType()+",brandName:"+brand.getBrandName()+"】");
		}
		Long brandId = sequenceService.getSequence("brand_id_seq");
		brand.setBrandId(brandId);
		brand.setState(ResConstant.BRAND_STATE_NORMAL);
		int num = mapper.insertBrand(brand);
		return num;
	}

	@Override
	public int updateBrand(Brand brand) {
		if(brand.getBrandId()==null){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		int num = mapper.updateBrand(brand);
		return num;
	}

	@Override
	public Brand getBrandById(Long brandId) {
		Brand brand = mapper.getByBrandId(brandId);
		return brand;
	}

	@Override
	public int deleteBrand(Brand brand) {
		brand.setState(ResConstant.BRAND_STATE_DELETED);
		int num = mapper.deleteBrand(brand);
		return num;
	}

	@Override
	public List<Brand> findBrandsByType(String eqptType) {
		List<Brand> brandList = mapper.findBrandsByType(eqptType);
		return brandList;
	}

	@Override
	public Brand getBrandByEqptTypeAndBrandName(String eqptType, String brandName) {
		List<Brand> brandList = mapper.getBrandByEqptTypeAndBrandName(eqptType, brandName);
		if(brandList==null || brandList.isEmpty()) throw new UserException("根据设备类型【"+eqptType+"】和品牌名称【"+brandName+"】没有获取到品牌信息");
		if(brandList.size()>1) throw new UserException("根据设备类型【"+eqptType+"】和品牌名称【"+brandName+"】获取到多个品牌");
		
		return brandList.get(0);
	}

}
